<!doctype html public "-//W3C//DTD HTML 4.0 Frameset//EN""http://www.w3.org/TR/REC-html40/frameset.dtd">
<html>
<head>
<title>
JsWs Overview
</title>
<link rel ="stylesheet" type="text/css" href="stylesheet.css" title="Style">
<script>
function asd() {
	
		parent.document.title="request.js Overview";
	
}
</script>
</head>
<body bgcolor="white" onload="asd();">

<!-- ========== START OF NAVBAR ========== -->
<a name="navbar_top"><!-- --></a>
<table border="0" width="100%" cellpadding="1" cellspacing="0">
<tr>
<td colspan=2 bgcolor="#EEEEFF" class="NavBarCell1">
<a name="navbar_top_firstrow"><!-- --></a>
<table border="0" cellpadding="0" cellspacing="3">
  <tr align="center" valign="top">
  
  
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="overview-summary.html"><font class="NavBarFont1"><b>Overview</b></font></a>&nbsp;</td>
  <td bgcolor="#FFFFFF" class="NavBarCell1Rev">	&nbsp;<font class="NavBarFont1Rev"><b>File</b></font>&nbsp;</td>
  

  <td bgcolor="#FFFFFF" class="NavBarCell1"> 	<font class="NavBarFont1">Class</font>&nbsp;</td>
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="overview-tree.html"><font class="NavBarFont1"><b>Tree</b></font></a>&nbsp;</td>
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="index-all.html"--><font class="NavBarFont1"><b>Index</b></font></a>&nbsp;</td>
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="help-doc.html"><font class="NavBarFont1"><b>Help</b></font></a>&nbsp;</td>
  </tr>
</table>
</td>
<td bgcolor="#EEEEFF" align="right" valign="top">
<em>
<b>JsWs</b></em>
</td>
</tr>

<tr>
<td bgcolor="white" class="NavBarCell2"><font size="-2">
&nbsp;PREV&nbsp;
&nbsp;NEXT</font></td>
<td bgcolor="white" class="NavBarCell2"><font size="-2">
  <a href="index.html" target="_top"><b>FRAMES</b></a>  &nbsp;
&nbsp;<a href="overview-summary.html" target="_top"><b>NO FRAMES</b></a>
&nbsp;&nbsp;
<script>
  <!--
  if(window==top) {
    document.writeln('<A HREF="allclasses-noframe.html" TARGET=""><B>All Classes</B></A>');
  }
  //-->
</script>
<noscript>
<a href="allclasses-noframe.html" target=""><b>All Classes</b></a>
</noscript>
</font></td>
</tr>
</table>
<!-- =========== END OF NAVBAR =========== -->

<hr>
<center>
	
	   <h2>request.js</h2>
	
</center>

	


<h4>Summary</h4>
<p>
	
		No overview generated for 'request.js'<BR/><BR/>
	
</p>

<hr>


    <table border="1" cellpadding="3" cellspacing="0" width="100%">
    <tr bgcolor="#CCCCFF" class="TableHeadingColor">
    <td colspan=2><font size="+2">
    
        <b>Class Summary</b>
    
    </font></td>
    </tr>
    
    <tr bgcolor="white" class="TableRowColor">
    <td width="15%"><b><a href="ExHttpParseError.html">ExHttpParseError</a></b></td>
    <td>Exception class for request parsing
 </td>
    </tr>
    
    <tr bgcolor="white" class="TableRowColor">
    <td width="15%"><b><a href="Request.html">Request</a></b></td>
    <td>Class to represent an HTTP request
 </td>
    </tr>
    
    </table>
    <hr/> 


<!-- ========== METHOD SUMMARY =========== -->

<!-- ========== END METHOD SUMMARY =========== -->


        <pre class="sourceview"><span class="comment">/* ***** BEGIN LICENSE BLOCK *****
 *   Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is JsWs.
 *
 * The Initial Developer of the Original Code is
 * Chris Winberry.
 * Portions created by the Initial Developer are Copyright (C) 2007
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 * 
 * ***** END LICENSE BLOCK ***** */</span>


<span class="comment">/**
 * <span class="attrib">@class</span> Exception class for request parsing
 * <span class="attrib">@constructor</span>
 * <span class="attrib">@base</span> ExBase
 * <span class="attrib">@param</span> {String} msg Description of exception
 * <span class="attrib">@param</span> {Number} code (optional) Error code
 * <span class="attrib">@param</span> {Exception} ex (optional) An earlier exception to wrap
 * <span class="attrib">@param</span> {Arguments} args (optional) Arguments array of a functions
 * <span class="attrib">@see</span> Request
 */</span>
<span class="reserved">function</span> ExHttpParseError (msg, code, ex) { ExHttpParseError.baseConstructor.call(<span class="reserved">this</span>, msg, code, ex); }
SubClass(ExBase, ExHttpParseError);


<span class="comment">/**
 * <span class="attrib">@class</span> Class to represent an HTTP request
 * <span class="attrib">@constructor</span>
 */</span>
<span class="reserved">function</span> Request () {
	<span class="comment">/**
	 * Headers collection
	 * <span class="attrib">@type</span> Hash
	 */</span>
	<span class="reserved">this</span>.headers = new Array();
	<span class="reserved">this</span>.headers[<span class="literal">'content-length'</span>] = 0;
	<span class="reserved">this</span>.headers[<span class="literal">'content-type'</span>] = <span class="literal">''</span>;
	<span class="comment">/**
	 * Cookies collection
	 * <span class="attrib">@type</span> Cookie[]
	 */</span>
	<span class="reserved">this</span>.cookies = new Array();
	<span class="comment">/**
	 * Querystring (GET) parameters
	 * <span class="attrib">@type</span> Querystring
	 */</span>
	<span class="reserved">this</span>.querystring = new Array();
	<span class="reserved">this</span>.watch(<span class="literal">"querystring"</span>, <span class="reserved">function</span> (property, oldVal, newVal) { <span class="reserved">return</span>(new Querystring(newVal)); });
	<span class="comment">/**
	 * Form (POST) parameters
	 * <span class="attrib">@type</span> Querystring
	 */</span>
	<span class="reserved">this</span>.form = new Array();
	<span class="reserved">this</span>.watch(<span class="literal">"form"</span>, <span class="reserved">function</span> (property, oldVal, newVal) { <span class="reserved">return</span>(new Querystring(newVal)); });
	<span class="comment">/**
	 * Request methods
	 * <span class="attrib">@type</span> String
	 */</span>
	<span class="reserved">this</span>.method = <span class="literal">"GET"</span>;
	<span class="comment">/**
	 * Complete path/querystring of the resource requested
	 * <span class="attrib">@type</span> String
	 */</span>
	<span class="reserved">this</span>.url = <span class="literal">"/"</span>;
	<span class="comment">/**
	 * HTTP version specified by the client
	 * <span class="attrib">@type</span> String
	 */</span>
	<span class="reserved">this</span>.httpVersion = <span class="literal">"HTTP/1.0"</span>;
	<span class="comment">/**
	 * Path to the resource requested
	 * <span class="attrib">@type</span> String
	 */</span>
	<span class="reserved">this</span>.path = <span class="literal">"/"</span>;
	<span class="comment">/**
	 * Complete, unparsed request
	 * <span class="attrib">@type</span> String
	 */</span>
	<span class="reserved">this</span>.raw = <span class="literal">""</span>;
	<span class="comment">/**
	 * Complete, unparsed headers
	 * <span class="attrib">@type</span> String
	 */</span>
	<span class="reserved">this</span>.rawHeaders = <span class="literal">""</span>;
	<span class="comment">/**
	 * Complete, unparsed querystring
	 * <span class="attrib">@type</span> String
	 */</span>
	<span class="reserved">this</span>.rawQuerystring = <span class="literal">""</span>;
	<span class="comment">/**
	 * Complete, unparsed body
	 * <span class="attrib">@type</span> String
	 */</span>
	<span class="reserved">this</span>.rawBody = <span class="literal">""</span>;

	<span class="comment">/** <span class="attrib">@private</span> */</span>
	<span class="reserved">this</span>.headersComplete = false;
}


<span class="comment">/**
 * Attempts to parse a raw request. Returns false until it is successful.
 * <span class="attrib">@param</span> {String} sData Data from the incoming request. Data is dealt with
 *  in a chunked fashion (&lt;i&gt;Request&lt;/i&gt; maintains all the data internally) so if
 *  calling &lt;i&gt;parse()&lt;/i&gt; more than once, only new data should be passed each time. 
 * <span class="attrib">@returns</span> True/False indicating whether the request has been completely parsed
 * <span class="attrib">@type</span> Boolean
 */</span>
Request.<span class="reserved">prototype</span>.parse = <span class="reserved">function</span> (sData) {
	<span class="reserved">if</span> (sData != null &amp;&amp; sData.length) {
		<span class="reserved">this</span>.raw += sData;
	}
	<span class="reserved">if</span> (!<span class="reserved">this</span>.raw.match(/(\r\n\r\n|\r\r|\n\n)/)) {
		<span class="reserved">return</span>(false);
	}

	<span class="reserved">if</span> (!<span class="reserved">this</span>.headersComplete) {
		[<span class="reserved">this</span>.rawHeaders, <span class="reserved">this</span>.rawBody] = <span class="reserved">this</span>.raw.split(/(?:\r\n\r\n|\r\r|\n\n)/);

		var headers = <span class="reserved">this</span>.rawHeaders.split(/(?:\r\n|\r|\n)/); <span class="comment">// TODO: handle multiline headers</span>
		<span class="reserved">if</span> (!headers.length) { throw(new ExHttpParseError(<span class="literal">"Unable to find headers in request [["</span> + <span class="reserved">this</span>.rawHeaders + <span class="literal">"]]"</span>)); }

		[
			<span class="reserved">this</span>.method,
			<span class="reserved">this</span>.url,
			<span class="reserved">this</span>.httpVersion
			] = headers[0].split(/\s/);
		<span class="reserved">if</span> (<span class="reserved">this</span>.method == null || <span class="reserved">this</span>.url == null || <span class="reserved">this</span>.httpVersion == null) {
 			throw(new ExHttpParseError(<span class="literal">"Unable to find request method and url [["</span>+ headers[0] +<span class="literal">"]]"</span>));
		}
		<span class="reserved">this</span>.method = <span class="reserved">this</span>.method.toLowerCase();

		[<span class="reserved">this</span>.path, <span class="reserved">this</span>.rawQuerystring] = <span class="reserved">this</span>.url.split(/\?/);
		<span class="reserved">this</span>.querystring = <span class="reserved">this</span>.rawQuerystring;

		<span class="reserved">for</span> (var i = 1; i &lt; headers.length; i++) {
			var pieces = headers[i].split(/\:\s*/);
			<span class="reserved">if</span> (pieces.length &lt; 2) { JsWs.log(<span class="literal">"Error on header split: "</span> + headers[i]); continue; }
			<span class="reserved">this</span>.headers[pieces.shift().toLowerCase()] = pieces.join(<span class="literal">':'</span>);
		}

		<span class="reserved">this</span>.cookies = Cookie.parse(<span class="reserved">this</span>.headers[<span class="literal">"cookie"</span>]);

		<span class="reserved">this</span>.headersComplete = true;
	}
	<span class="reserved">else</span> {
		<span class="reserved">this</span>.rawBody += sData;
	}

	<span class="reserved">if</span> (
		(<span class="reserved">this</span>.method == <span class="literal">"post"</span>)
		&amp;&amp;
		(<span class="reserved">this</span>.headers[<span class="literal">'content-length'</span>] == <span class="reserved">this</span>.rawBody.length)
		&amp;&amp;
		(<span class="reserved">this</span>.headers[<span class="literal">'content-type'</span>].indexOf(<span class="literal">"application/x-www-form-urlencoded"</span>) &gt; -1)
		){
		<span class="reserved">this</span>.form = <span class="reserved">this</span>.rawBody;
	}

	<span class="reserved">return</span>(
		<span class="reserved">this</span>.headersComplete &amp;&amp;
		(
			<span class="reserved">this</span>.headers[<span class="literal">'content-length'</span>] == null
			||
			<span class="reserved">this</span>.headers[<span class="literal">'content-length'</span>] == <span class="reserved">this</span>.rawBody.length
			)
		);
}


<span class="comment">/**
 * Runs cleanup on internal objects
 */</span>
Request.<span class="reserved">prototype</span>.destroy = <span class="reserved">function</span> () {
	<span class="reserved">this</span>.headers = null;
	<span class="reserved">for</span> (var i = 0; i &lt; <span class="reserved">this</span>.cookies.length; i++) {
		<span class="reserved">this</span>.cookies[i].destroy();
		<span class="reserved">this</span>.cookies[i] = null;
	}
	<span class="reserved">this</span>.cookies = null;
	<span class="reserved">this</span>.unwatch(<span class="literal">"querystring"</span>);
	<span class="reserved">this</span>.querystring = null;
	<span class="reserved">this</span>.unwatch(<span class="literal">"form"</span>);
	<span class="reserved">this</span>.form = null;
	<span class="reserved">this</span>.path = null;
	<span class="reserved">this</span>.raw = null;
	<span class="reserved">this</span>.rawHeaders = null;
	<span class="reserved">this</span>.rawQuerystring = null;
	<span class="reserved">this</span>.rawBody = null;
}

</pre>
	<hr>



<!-- ========== START OF NAVBAR ========== -->
<a name="navbar_top"><!-- --></a>
<table border="0" width="100%" cellpadding="1" cellspacing="0">
<tr>
<td colspan=2 bgcolor="#EEEEFF" class="NavBarCell1">
<a name="navbar_top_firstrow"><!-- --></a>
<table border="0" cellpadding="0" cellspacing="3">
  <tr align="center" valign="top">
  
  
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="overview-summary.html"><font class="NavBarFont1"><b>Overview</b></font></a>&nbsp;</td>
  <td bgcolor="#FFFFFF" class="NavBarCell1Rev">	&nbsp;<font class="NavBarFont1Rev"><b>File</b></font>&nbsp;</td>
  

  <td bgcolor="#FFFFFF" class="NavBarCell1"> <font class="NavBarFont1">Class</font>&nbsp;</td>
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="overview-tree.html"><font class="NavBarFont1"><b>Tree</b></font></a>&nbsp;</td>
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="index-all.html"--><font class="NavBarFont1"><b>Index</b></font></a>&nbsp;</td>
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="help-doc.html"><font class="NavBarFont1"><b>Help</b></font></a>&nbsp;</td>
  </tr>
</table>
</td>
<td bgcolor="#EEEEFF" align="right" valign="top"><em>
<b>JsWs</b></em>
</td>
</tr>

<tr>
<td bgcolor="white" class="NavBarCell2"><font size="-2">
&nbsp;PREV&nbsp;
&nbsp;NEXT</font></td>
<td bgcolor="white" class="NavBarCell2"><font size="-2">
  <a href="index.html" target="_top"><b>FRAMES</b></a>  &nbsp;
&nbsp;<a href="overview-summary.html" target="_top"><b>NO FRAMES</b></a>
&nbsp;&nbsp;
<script>
  <!--
  if(window==top) {
    document.writeln('<A HREF="allclasses-noframe.html" TARGET=""><B>All Classes</B></A>');
  }
  //-->
</script>
<noscript>
<a href="allclasses-noframe.html" target=""><b>All Classes</b></a>
</noscript>
</font></td>
</tr>
</table>
<!-- =========== END OF NAVBAR =========== -->

<hr>
<font size="-1">

</font>
<div class="jsdoc_ctime">Documentation generated by <a href="http://jsdoc.sourceforge.net/" target="_parent">JSDoc</a> on Thu Nov  8 22:50:16 2007</div>
</body>
</html>
